/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.entity;

import entity.Courses;
import entity.Lecture;
import java.util.List;
import javax.faces.context.FacesContext;
import jpa.controllers.LectureJpaController;
import jsf.util.JsfUtil;

/**
 *
 * @author atap
 */
public class LecturesController {
    
    private Lecture lecture = null;
    private LectureJpaController jpaController = null;
    private LecturesConverter converter = null;
    
    public LecturesController() {
        FacesContext context = FacesContext.getCurrentInstance();
        jpaController = (LectureJpaController) context.getApplication().getELResolver().getValue(context.getELContext(), null, "lectureJpa");
        converter = new LecturesConverter();
    }
    
    public Lecture getLecture() {
        if(lecture == null) {
            lecture = (Lecture)JsfUtil.getObjectFromRequestParameter("jsfcrud.currentLectures", converter, null);
        }
        if(lecture == null) {
            lecture = new Lecture();
        }
        return lecture;
    }
    
    public List<Lecture> getAllLEctures() {
        return jpaController.findLectureEntities();
    }
    
     public Courses course;
 
    public void setAction(Courses action) {
            this.course = action;
    }
    
     public String create() {
        try {           
            Courses c = course;
            //course = new Courses(courseid);
            lecture.setCourseId(c);
            jpaController.create(lecture);
            
            JsfUtil.addSuccessMessage("Lecture was successfully created.");
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return null;
    }
}
